<?php
/**
 * admin service file Created by PhpStorm.
 * User: yang
 * Date: 2020/06/09
 */

namespace App\Http\Service\Admin;

use Common\Repository\ActionLogRepository;

class ActionLogService
{
    /**
     * 获取列表
     *
     * @param array $get_data
     * @return array
     */
    public function getList($get_data = [])
    {
        try {
            if (isset($get_data['field']) && isset($get_data['order'])) {
                $arr_option['order'] = ['key' => $get_data['field'], 'value' => $get_data['order']];
            }
            $arr_option['limit'] = $get_data['limit'];
            $arr_option['where'] = $get_data['where'];

            $result = ActionLogRepository::getList($arr_option);
            $list = list_data_page($result);

            return $list;
        } catch (\Exception $exception) {
            return hello_error('失败！', $exception->getMessage());
        }
    }

    /**
     * 添加操作
     *
     * @param array $post_data
     * @return array
     */
    public function insert($post_data = [])
    {
        try {
            $result = ActionLogRepository::addOne($post_data);

            if ($result) {
                return hello_success('添加成功！', $result);
            }

            return hello_error('添加失败！');
        } catch (\Exception $exception) {
            return hello_error('失败！', $exception->getMessage());
        }
    }

    /**
     * 编辑视图
     *
     * @param int $id
     * @return array
     */
    public function edit(int $id)
    {
        try {
            if ($id == 0) {
                error_prompt('参数错误');
            }
            $result = ActionLogRepository::findOne($id);

            if ($result) {
                return hello_success('获取成功！', $result);
            }

            return hello_error('获取失败！');
        } catch (\Exception $exception) {
            return hello_error('失败！', $exception->getMessage());
        }
    }

    /**
     * 编辑操作
     *
     * @param array $post_data
     * @return array
     */
    public function update($post_data = [])
    {
        try {
            $result = ActionLogRepository::editOne($post_data);

            if ($result) {
                return hello_success('编辑成功！', $result);
            }

            return hello_error('编辑失败！');
        } catch (\Exception $exception) {
            return hello_error('失败！', $exception->getMessage());
        }
    }

    /**
     * 删除操作
     *
     * @param int $id
     * @return array
     */
    public function delete(int $id)
    {
        try {
            if ($id == 0) {
                error_prompt('参数错误');
            }
            $result = ActionLogRepository::deleteOne($id);

            if ($result) {
                return hello_success('删除成功！', $result);
            }

            return hello_error('删除失败失败！');
        } catch (\Exception $exception) {
            return hello_error('失败！', $exception->getMessage());
        }
    }

    /**修改状态
     *
     * @param array $post_data
     * @return array
     */
    public function editStatus($post_data = [])
    {
        try {
            $up_status = [
                'id' => $post_data['id'],
                'status' => $post_data['status'] == 1 ? 0 : 1
            ];

            $result = ActionLogRepository::editOne($up_status);

            if ($result) {
                return hello_success('修改成功！', $up_status['status']);
            }

            return hello_error('修改失败！');
        } catch (\Exception $exception) {
            return hello_error('失败！', $exception->getMessage());
        }
    }

}